﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Container
 * 唯一标识：1bb381e8-2302-41b7-9f4a-4749f54f4189
 * 文件名：Descriptions
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/27 17:22:15
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 描述列表容器控件
/// </summary>
public sealed class Descriptions : ContainerControlBase<DescriptionItem>
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Descriptions()
    {
        Columns = 1;
    }

    public override List<DescriptionItem> Children { get; internal set; }
    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        Param par = new Param();

        if (HasBorder) par.Add("border");

        sb.AppendLine($"<el-descriptions title=\"{Title}\" :column=\"{Columns}\" direction=\"{Direction.GetDescription()}\" {par}>");

        foreach (var item in Children)
        {
            sb.AppendLine(item.ConverToHtml());
        }

        sb.AppendLine($"</el-descriptions>");

        return sb.ToString();
    }

    /// <summary>
    /// 列数
    /// </summary>
    internal int Columns { get; set; }

    /// <summary>
    /// 是否包含边框
    /// </summary>
    internal bool HasBorder { get; set; }

    /// <summary>
    /// 排列方式
    /// </summary>
    internal DirectionType Direction { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建描述列表
    /// </summary>
    /// <param name="title"></param>
    /// <param name="children"></param>
    /// <param name="columns"></param>
    /// <param name="hasBorder"></param>
    /// <param name="direction"></param>
    /// <returns></returns>
    public static Descriptions CreateDescriptions(string title, Action<List<DescriptionItem>> children, int columns = 1, bool hasBorder = true, DirectionType direction = DirectionType.Vertical)
    {
        Descriptions descriptions = new Descriptions()
        {
            Id = GenerateId(),
            Title = title,
            Columns = columns,
            HasBorder = hasBorder,
            Direction = direction,
            Children = new List<DescriptionItem>()
        };

        children?.Invoke(descriptions.Children);
        return descriptions;
    }

    /// <summary>
    /// 将描述列表单项控件添加到父控件中
    /// </summary>
    /// <param name="children"></param>
    /// <param name="item"></param>
    public static void AddToChildren(this DescriptionItem item, List<DescriptionItem> children)
    {
        children.Add(item);
    }

    /// <summary>
    /// 创建描述列名单项控件
    /// </summary>
    /// <param name="title"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public static DescriptionItem CreateDescriptionItem(string title, string content, AlignType align = AlignType.Left, AlignType labelAlign = AlignType.Right)
    {
        DescriptionItem item = new DescriptionItem()
        {
            Id = GenerateId(),
            Title = title,
            Content = content,
            Align = align,
            LabelAlign = labelAlign,
        };

        return item;
    }

    /// <summary>
    /// 创建描述列表单项控件
    /// </summary>
    /// <param name="title"></param>
    /// <param name="tag"></param>
    /// <returns></returns>
    public static DescriptionItem CreateDescriptionItem(string title, Func<ControlBase> tag, AlignType align= AlignType.Left, AlignType labelAlign= AlignType.Right)
    {
        var control = tag?.Invoke();

        return CreateDescriptionItem(title, control.ConverToHtml(), align,labelAlign);
    }
}

/// <summary>
/// 描述列表单项控件
/// </summary>
public sealed class DescriptionItem : ControlBase
{
    internal DescriptionItem()
    {
    }

    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<el-descriptions-item label=\"{Title}\" label-align=\"{LabelAlign}\" align=\"{Align}\">");
        sb.AppendLine(Content);
        sb.AppendLine($"</el-descriptions-item>");

        return sb.ToString();
    }

    /// <summary>
    /// 内容
    /// </summary>
    internal string Content { get; set; }

    /// <summary>
    /// 列的内容对齐方式（如无 border，对标签和内容均生效）
    /// </summary>
    internal AlignType Align { get; set; }

    /// <summary>
    /// 列的标签对齐方式，若不设置该项，则使用内容的对齐方式（如无 border，请使用 align 参数）
    /// </summary>
    internal AlignType LabelAlign { get; set; }
}

/// <summary>
/// 对齐方式
/// </summary>
public enum AlignType
{
    /// <summary>
    /// 靠左
    /// </summary>
    [Description("left")]
    Left,
    /// <summary>
    /// 居中
    /// </summary>
    [Description("center")]
    Center,
    /// <summary>
    /// 靠右
    /// </summary>
    [Description("right")]
    Right
}